package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class CountNumGenerator extends Generator{
	protected Domain sampleDomain;

	public CountNumGenerator(){
		super();
		super.fileName = "count_num.rs";
		super.standardName = "CountNum";
	}
	
	public Domain getSampleDomain() {
		return sampleDomain;
	}

	public void setSampleDomain(Domain sampleDomain) {
		this.sampleDomain = sampleDomain;
	}


	@Override
	public StatementList generateStatementList() {
		String dbRow = "MySqlRow";
		if ("PostgreSQL".equalsIgnoreCase(sampleDomain.getDbType())||"pgsql".equalsIgnoreCase(sampleDomain.getDbType())) {
			dbRow = "PgRow";
		}
		List<Writeable> sList = new ArrayList<Writeable>();
		long serial = 1000L;
		if ("MariaDB".equalsIgnoreCase(sampleDomain.getDbType())||"MySQL".equalsIgnoreCase(sampleDomain.getDbType())) {
			sList.add(new Statement(1000L,0,"use sqlx::mysql::MySqlRow;"));
		}else if ("PostgreSQL".equalsIgnoreCase(sampleDomain.getDbType())||"pgsql".equalsIgnoreCase(sampleDomain.getDbType())) {
			sList.add(new Statement(1000L,0,"use sqlx::postgres::PgRow;"));
		}
		sList.add(new Statement(2000L,0,"use sqlx::{FromRow, Row};"));
		sList.add(new Statement(3000L,0,""));
		sList.add(new Statement(4000L,0,"pub struct CountNum{"));
		sList.add(new Statement(5000L,1,"pub count_num: i64,"));
		sList.add(new Statement(6000L,0,"}"));
		sList.add(new Statement(7000L,0,""));
		sList.add(new Statement(8000L,0,"impl<'c> FromRow<'c, "+dbRow+"<'c>> for CountNum {"));
		sList.add(new Statement(9000L,1,"fn from_row(row: &"+dbRow+") -> Result<CountNum, sqlx::Error> {"));
		sList.add(new Statement(10000L,2,"Ok(CountNum{"));
		sList.add(new Statement(11000L,3,"count_num:row.get(0),"));
		sList.add(new Statement(12000L,2,"})"));
		sList.add(new Statement(13000L,1,"}"));
		sList.add(new Statement(14000L,0,"}"));
		
		sList.add(new Statement(15000L,0,""));
		sList.add(new Statement(16000L,0,"pub fn get_image_base64(data:Option<Vec<u8>>)->String{"));
		sList.add(new Statement(17000L,1,"match data {"));
		sList.add(new Statement(18000L,2,"Some(data) => {"));
		sList.add(new Statement(19000L,3,"base64::encode(data)"));
		sList.add(new Statement(20000L,2,"},"));
		sList.add(new Statement(21000L,2,"None => \"\".to_string()"));
		sList.add(new Statement(22000L,1,"}"));
		sList.add(new Statement(23000L,0,"}"));

		return WriteableUtil.merge(sList);
	}

}
